﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using MongoServices.Tests.TestHelpers;
using MongoServices.Tests.Stubs;

namespace MongoServices.Tests.MongoServicesModelTests
{
	public class MongoCollectionNameAttributeTests : TestBase
	{
		protected ReadWriteRepository<TestModelSimple> ReadWriteRepositoryTestModelSimple { get; private set; }
		protected ReadWriteRepository<TestModelWithoutCollectionNameAttributeBase> ReadWriteRepositoryTestModelWithoutCollectionNameAttriuteBase { get; set; }

		public MongoCollectionNameAttributeTests()
		{
			Setup();
		}

		private void Setup()
		{
			ReadWriteRepositoryTestModelSimple = new ReadWriteRepositoryTestModelSimpleStub();
			ReadWriteRepositoryTestModelWithoutCollectionNameAttriuteBase = new ReadWriteRepositoryTestModelWithoutCollectionNameAttriuteBaseStub();
		}
	}

	public class WhenMongoCollectionNameAttributeIsApplied : MongoCollectionNameAttributeTests
	{
		[Fact]
		public void TheModelWillBeSavedWithoutAnException()
		{
			var testData = "TestData";
			var model = new TestModelSimple { TestField = testData };

			Assert.ThrowsDelegate saveDelegate = delegate { ReadWriteRepositoryTestModelSimple.Save(model); };

			Assert.DoesNotThrow(saveDelegate);
		}
	}

	public class WhenMongoCollectionNameAttributeIsNotApplied : MongoCollectionNameAttributeTests
	{
		[Fact]
		public void MongoCollectionNameMissingExceptionWillBeThrown()
		{
			var testData = "TestData";
			var model = new TestModelWithoutCollectionNameAttributeInherited { TestField = testData };

			Assert.ThrowsDelegate saveDelegate = delegate { ReadWriteRepositoryTestModelWithoutCollectionNameAttriuteBase.Save(model); };

			Assert.Throws<MongoCollectionNameMissingException>(saveDelegate);
		}
	}
}
